#region Copyright
// Copyright (c) 2010, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>.
// This source is subject to the Microsoft Public License. 
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 
// All other rights reserved.
#endregion

namespace LiveWriter.Plugins.Snippet
{
    using System;
    using System.Windows.Forms;

    internal partial class CodeEditor : CodeEditorBase
    {
        public CodeEditor()
        {
            InitializeComponent();
            cboLanguages.Load(SupportedSettings.Languages);
            cboStyles.Load(SupportedSettings.Styles);
        }

        protected override void LoadProperties()
        {
            cboLanguages.SetSelectedIndex(Properties.Language());
            cboStyles.SetSelectedIndex(Properties.Style());
            txtCode.Text = Properties.Code();
            chkShowLine.Checked = Properties.ShowLineNumber();
            chkTransparent.Checked = Properties.Transparent();
            txtCaption.Text = Properties.Caption();
        }

        protected override void SaveProperties()
        {
            Properties.Language(((Pair) cboLanguages.SelectedItem).Key);
            Properties.Style(((Pair)cboStyles.SelectedItem).Key);
            Properties.Code(txtCode.Text.Trim());
            Properties.ShowLineNumber(chkShowLine.Checked);
            Properties.Transparent(chkTransparent.Checked);
            Properties.Caption(txtCaption.Text.Trim());
        }

        private void FixIndentation()
        {
            txtCode.Text = txtCode.Text.FixIndentation(4);
            txtCode.SelectionStart = 0;
            txtCode.SelectionLength = 0;
        }

        private void btnIndentation_Click(object sender, EventArgs e)
        {
            FixIndentation();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            SaveProperties();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}